from fastapi import APIRouter, HTTPException, UploadFile, File, Form, Request
from pojo.results import Result
from service.document_service import question_generate_imp
from utils.log import logger

document = APIRouter(prefix="/document")


@document.post("/questionGenerate")
async def question_generate(
        file: UploadFile = File(...),  # 接收上传的文件
        type: int = Form(...),  # 接收int类型作为参数，1为单选，2为多选，3为判断
        complexity: int = Form(...)  # 接收int类型作为参数，1为容易，2为中等，3为难

):
    # 参数校验
    if not all([file, type, complexity]):
        raise HTTPException(status_code=400, detail='缺少必要参数')

    logger.info(f"拿到的文档名称为{file.filename},类型为{type},难度为{complexity}")
    try:
        data = await question_generate_imp(file, type, complexity)
        return vars(Result(200, "生成成功", data))
    except Exception as E:
        return vars(Result(500, "生成题目出错", E))


@document.get("/")
async def root():
    return {"message": "Hello World"}
